#include <stdio.h>
#include "isLeapYear.h"
#include "SumDays.h"
#include "Week.h"
#include "printrow.h"

void printfrow(int year)
{	
	//大二维数组
	int a[28][24] = {0};
	int i,j;
	int da1 = 1,da2 = 1,da3 = 1,da4 = 1,da5 = 1,da6 = 1,da7 = 1,da8 = 1,da9 = 1,da10 = 1,da11 = 1,da12 = 1;
	//一、二、三月
	for(i = 0;i < 6;i++)
	{
		//第一排每月一号星期
		if(i == 0)
		{
			//一月
			for(j = Week(1,year);j < 7;++j)
			{
				a[i][j] = da1;
				++da1;
			}
			//二月
			for(j = (Week(2,year) + 8);j < 15;++j)
			{
				a[i][j] = da2;
				++da2;
			}
			//三月
			for(j = (Week(3,year) + 16);j < 23;++j)
			{
				a[i][j] = da3;
				++da3;
			}
		}
		//后面日期
		else
		{
			//一月
			for(j = 0;j < 7;++j)
			{
				if(da1 <= 31)
				{
					a[i][j] = da1;
					++da1;
				}
			}
			//二月
			for(j = 8;j < 15;++j)
			{
				//闰年
				if(isLeapYear(year) > 0 && da2 <= 29)
				{
					a[i][j] = da2;
					++da2;
				}
				//平年
				if(isLeapYear(year) == 0 &&  da2 <= 28)
				{
					a[i][j] = da2;
					++da2;
				}
			}
			//三月
			for(j = 16;j < 23;++j)
			{
				if(da3 <= 31)
				{
					a[i][j] = da3;
					++da3;
				}
			}
		}
	}
	//四、五、六月
	for(i = 7;i < 13;i++)
	{
		if(i == 7)
		{
			for(j = Week(4,year);j < 7;++j)
			{
				a[i][j] = da4;
				++da4;
			}
			for(j = (Week(5,year) + 8);j < 15;++j)
			{
				a[i][j] = da5;
				++da5;
			}
			for(j = (Week(6,year) + 16);j < 23;++j)
			{
				a[i][j] = da6;
				++da6;
			}
		}
		else
		{
			for(j = 0;j < 7;++j)
			{
				if(da4 <= 30)
				{
					a[i][j] = da4;
					++da4;
				}
			}
			for(j = 8;j < 15;++j)
			{
				if(da5 <= 31)
				{
					a[i][j] = da5;
					++da5;
				}
			}
			for(j = 16;j < 23;++j)
			{
				if(da6 <= 30)
				{
					a[i][j] = da6;
					++da6;
				}
			}
		}
	}
	//七、八、九月
	for(i = 14;i < 20;i++)
	{
		if(i == 14)
		{
			for(j = Week(7,year);j < 7;++j)
			{
				a[i][j] = da7;
				++da7;
			}
			for(j = (Week(8,year) + 8);j < 15;++j)
			{
				a[i][j] = da8;
				++da8;
			}
			for(j = (Week(9,year) + 16);j < 23;++j)
			{
				a[i][j] = da9;
				++da9;
			}
		}
		else
		{
			for(j = 0;j < 7;++j)
			{
				if(da7 <= 31)
				{
					a[i][j] = da7;
					++da7;
				}
			}
			for(j = 8;j < 15;++j)
			{
				if(da8 <= 31)
				{
					a[i][j] = da8;
					++da8;
				}
			}
			for(j = 16;j < 23;++j)
			{
				if(da9 <= 30)
				{
					a[i][j] = da9;
					++da9;
				}
			}
		}
	}
	//十、十一、十二月
	for(i = 21;i < 27;i++)
	{
		if(i == 21)
		{
			for(j = Week(10,year);j < 7;++j)
			{
				a[i][j] = da10;
				++da10;
			}
			for(j = (Week(11,year) + 8);j < 15;++j)
			{
				a[i][j] = da11;
				++da11;
			}
			for(j = (Week(12,year) + 16);j < 23;++j)
			{
				a[i][j] = da12;
				++da12;
			}
		}
		else
		{
			for(j = 0;j < 7;++j)
			{
				if(da10 <= 31)
				{
					a[i][j] = da10;
					++da10;
				}
			}
			for(j = 8;j < 15;++j)
			{
				if(da11 <= 30)
				{
					a[i][j] = da11;
					++da11;
				}
			}
			for(j = 16;j < 23;++j)
			{
				if(da12 <= 31)
				{
					a[i][j] = da12;
					++da12;
				}
			}
		}
	}

	for(i = 0;i < 28;++i)
	{
		if(i == 0)
		{
			printf("            一月   %d                                        二月   %d                                        三月   %d\n",year,year,year);
			printf("Sun   Mon   Tue   Wed   Thu   Fri   Sat       \t Sun   Mon   Tue   Wed   Thu   Fri   Sat         Sun   Mon   Tue   Wed   Thu   Fri   Sat\n");

		}
		if(i == 7)
		{
			printf("            四月   %d                                        五月   %d                                        六月   %d\n",year,year,year);
			printf("Sun   Mon   Tue   Wed   Thu   Fri   Sat       \t Sun   Mon   Tue   Wed   Thu   Fri   Sat         Sun   Mon   Tue   Wed   Thu   Fri   Sat\n");
		}
		else if(i == 14)
		{
			printf("            七月   %d                                        八月   %d                                        九月   %d\n",year,year,year);
			printf("Sun   Mon   Tue   Wed   Thu   Fri   Sat       \t Sun   Mon   Tue   Wed   Thu   Fri   Sat         Sun   Mon   Tue   Wed   Thu   Fri   Sat\n");	
		}
		else if(i == 21)
		{
			printf("            十月   %d                                        十一月   %d                                      十二月   %d\n",year,year,year);
			printf("Sun   Mon   Tue   Wed   Thu   Fri   Sat       \t Sun   Mon   Tue   Wed   Thu   Fri   Sat         Sun   Mon   Tue   Wed   Thu   Fri   Sat\n");
		}
		for(j = 0;j < 24;++j)
		{
			if(a[i][j] != 0)
			{
				printf("%3d   ",a[i][j]);
			}
			else
			{
				printf("      ");
			}
		}
		printf("\n");
	}
}



